package com.yjhb.android.sbz.mvp.feedback;

import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.http.UploadTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2021/1/4 17:07
 */
public class FeedbackPresenter extends ConventionPresenter<FeedbackView> {
    private final String TAG = "FeedbackPresenter";

    public FeedbackPresenter(FeedbackView objectCommonView) {
        super(objectCommonView);
    }

    @Override
    protected void onDestroy() {

    }


    /**
     * 反馈
     *
     * @param title
     * @param content
     * @param paths
     */
    public void feedback(String title, String content, List<LocalMedia> paths) {
        if (view != null) {
            view.onWait("图片上传中...");
        }
        List<String> urls = new ArrayList<>();
        UploadTool.OSSUploadImages(paths,false, new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(String s) {
                urls.add(s);
            }

            @Override
            public void onError(Throwable e) {
                if (view != null) {
                    view.onFailure(-1, e.getMessage());
                }
            }
            @Override
            public void onComplete() {
                if (view != null) {
                    view.onWait("正在提交...");
                }
                HttpTool.request(model.feedback(title, content, urls), new HttpObserver<Object>() {
                    @Override
                    public void onResult(Object data) {
                        if (view != null) {
                            view.onSuccess();
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        if (view != null) {
                            view.onFailure(code, msg);
                        }
                    }
                });
            }
        });

    }
}
